package com.imchen.cms.cmscore.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imchen.cms.cmscore.dao.CommentsOwnTypeMapper;
import com.imchen.cms.cmscore.model.CommentsOwnType;
import com.imchen.cms.constant.CMS;
import com.imchen.cms.cmscore.dao.CommentsMapper;
import com.imchen.cms.cmscore.model.Comments;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author imchen
 * @create: 2019-01-15 13:59
 **/
@Service
public class CommentsService implements BaseService<Comments> {

    private Logger logger = LoggerFactory.getLogger(CommentsService.class);

    @Autowired
    private CommentsMapper mapper;

    @Autowired
    private CommentsOwnTypeMapper commentsOwnTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Integer> add(Comments comments) {
        logger.debug("comments obj:{}", comments);
        HashMap<String, Integer> map = new HashMap<>();
        int count = mapper.insertSelective(comments);
        int id = comments.getId();
        if (count > 0) {
            String[] commentsTypesArr = comments.getTypes();
            if (commentsTypesArr.length > 0) {
                ArrayList<CommentsOwnType> commentsOwnTypeList = new ArrayList<>();
                for (String val : commentsTypesArr
                ) {
                    CommentsOwnType commentsOwnType = new CommentsOwnType();
                    commentsOwnType.setCommentsId(id);
                    commentsOwnType.setCommentsTypeId(Integer.parseInt(val));
                    commentsOwnTypeList.add(commentsOwnType);
                }
                count = commentsOwnTypeMapper.insertSelectiveList(commentsOwnTypeList);
            }
        }
        map.put("count", count);
        map.put("id", id);
        return map;
    }

    @Override
    public Comments findById(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int removeById(int id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Comments comments) {
        return mapper.updateByPrimaryKeySelective(comments);
    }

    public List<Comments> findByPage(int currentIndex, int pageSize) {
        return mapper.selectComments(currentIndex, pageSize);
    }

    @Override
    public int deleteById(int id) {
        return mapper.deleteByPrimaryKeyLogic(id, CMS.DATA_IS_DELETE);
    }

    public int restoreById(int id) {
        return mapper.deleteByPrimaryKeyLogic(id, CMS.DATA_NOT_DELETE);
    }

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public PageInfo<Comments> findByPageHelper(int index, int pageSize) {
        PageHelper.startPage(index, pageSize);
        List<Comments> commentsList = mapper.selectAllCommentsAndTypes();
        return new PageInfo<>(commentsList);
    }

    @Transactional(readOnly = true)
    public List<Comments> findByDate(String startTime, String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);
        return mapper.selectByBetweenDate(startDate, endDate);
    }
}